package com.kkb.servlet;

import com.kkb.pojo.user;
import com.kkb.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "loginServlet")
public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println(username+"-----"+password);
        String sql = "select * from tb_users where username = ? and password = ?";
        Object[] params = {username,password};
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            user user = queryRunner.query(sql, new BeanHandler<user>(user.class), params);
            if (user != null){
                System.out.println("登录成功！");
                response.getWriter().print("登录成功！");
            }else{
                System.out.println("登录失败！");
                response.getWriter().print("登录失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
